Create objects in the line of sight (based on cells)
The below code shows how to get the elements of type Line of Sight and place the object(here cell used) at object position of line of sight.
Note:
This example will only work for OpenRoads Designer version 10.12 and above
//Required References
using System;
using System.Collections.Generic;
using Bentley.MstnPlatformNET;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.CifNET.SDK;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.LinearGeometry;
using Element = Bentley.DgnPlatformNET.Elements.Element;
using System.Diagnostics;
using Bentley.Interop.MicroStationDGN;
public void CreateObjectsInLineOfSight()
{
try
{
//Get active DGN Model
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
ConsensusConnection consensusConnection = new ConsensusConnection(dgnModel);
//Get all line elements to get Line of sight objects
List<Element> elements = GetAllMSElements(MSElementType.Line);
foreach (Element element in elements)
{
FeaturizedModelEntity featurizedModelEntity = Bentley.CifNET.GeometryModel.SDK.FeaturizedModelEntity.CreateFromElement(consensusConnection, element);
//featurizedModelEntity.RepresentationOf
if (featurizedModelEntity is LinearEntity3d)
{
LinearEntity3d entity = featurizedModelEntity as LinearEntity3d;
if (entity.Alignment != null) continue;
//Get object position point from line of sight
LinearPoint objectPositionPoint = entity.LinearGeometry.EndPoint;
//Set the parameters for cell creation
string cellName = "Arrow - PvtMrk - Left";
double scale = 1.0;
//User might need unit conversion here based on settings
DPoint3d dpOrigin = new DPoint3d(objectPositionPoint.Coordinates);
Angle angle = Angle.FromDegrees(0.0);
//Place cell
CellElement cell = PlaceCellFromLibrary(cellName, dpOrigin, angle.Radians, scale);
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
public List<Element> GetAllMSElements(MSElementType ElementType)
{
List<Element> elmtsList = new List<Element>();
try
{
//Create element scanning criteria to get all elements of type ElementType
ScanCriteria scanCriteria = new ScanCriteria();
BitMask types = new BitMask(false);
types.Set((uint)ElementType - 1);
uint size = 1 + (uint)ElementType;
size = (size + 7) / 8;
size = (size * 16) - 15;
types.EnsureCapacity(size + 1); // To workaround bug in DgnPlatformNET
scanCriteria.SetElementTypeTest(types);
scanCriteria.SetModelRef(Session.Instance.GetActiveDgnModel());
scanCriteria.SetModelSections(DgnModelSections.GraphicElements);
ScanDelegate scanDelegate = (Element elmt, DgnModelRef modelRef) =>
{
elmtsList.Add(elmt);
return StatusInt.Success;
};
scanCriteria.Scan(scanDelegate);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return elmtsList;
}
public CellElement PlaceCellFromLibrary(string cellName, DPoint3d origin, double xyRotationRadians, double scale)
{
CellElement cellElement = null;
try
{
//Create origin point
Bentley.Interop.MicroStationDGN.Point3d iopOrigin = new Bentley.Interop.MicroStationDGN.Point3d
{
X = origin.X,
Y = origin.Y,
Z = origin.Z
};
//create scale point
Bentley.Interop.MicroStationDGN.Point3d iopScale = new Bentley.Interop.MicroStationDGN.Point3d
{
X = scale,
Y = scale,
Z = scale
};
//create rotation matrix
Bentley.Interop.MicroStationDGN.Matrix3d iopRotation = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.Matrix3dFromAxisAndRotationAngle(2, xyRotationRadians);
//Create cell element
cellElement = Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.CreateCellElement2(cellName, ref iopOrigin, ref iopScale, true, ref iopRotation);
if (cellElement != null)
{
//Add cell to active model
Bentley.MstnPlatformNET.InteropServices.Utilities.ComApp.ActiveModelReference.AddElement(cellElement);
}
return cellElement;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return cellElement;
}
}